package ga.crossover;
import java.util.Random;

import ga.model.Chromosome;


public class TwoPoint implements Crossover {
	Random rand= new Random();
	
	@Override
	public Chromosome[] crossover(Chromosome[] parents) {
		
		double rate= rand.nextDouble()*100
		if(rate>crossoverRate)
		{
			int index1=rand.nextInt(200);
			int index2=rand.nextInt(200);
			while(index1==index2)
				index2=rand.nextInt(200);
			
			//swap
			if(index2>index1){
				int index3=index1;
				index1=index2;
				index2=index3;
			}
			String str1=parents[0].getChromosome().substring(0,index1)
					+parents[1].getChromosome().substring(index1+1,index2)
					+parents[0].getChromosome().substring(1+index2);
			String str2=parents[1].getChromosome().substring(0,index1)
					+parents[0].getChromosome().substring(index1+1,index2)
					+parents[1].getChromosome().substring(1+index2);
			Chromosome[] childs={new Chromosome(str1),new Chromosome(str2)};
			
			return childs;
		}
		else 
			return parents;
	}

}
